#include "Lib9.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
namespace lib9bada {
Lib9::Lib9(Application* pApp) {
	defaultInit();
	Construct(FORM_STYLE_NORMAL);

	pFG = new Canvas();

	Rectangle rect = GetBounds();
	SCR_W = rect.width;
	SCR_H = rect.height;
	pFG->Construct(GetBounds());

	pFont = pFG->GetFontN();

	pL9Str = new L9Str();
	pL9Str->setSystemFont(pFont);

	setDisplay(pApp, this);

	//启动游戏主线程
	Thread* pThread = new Thread();
	pThread->Construct(*this);
	pThread->Start();
}

Lib9::~Lib9(void) {
	if (pFont != null) {
		delete pFont;
		pFont = null;
	}
	if (pFG != null) {
		delete pFG;
		pFG = null;
	}
	if (pL9Str != null) {
		delete pL9Str;
		pL9Str = null;
	}
	if (pDialogMsg != null) {
		delete pDialogMsg;
		pDialogMsg = null;
	}
	if (pDialogYesNo != null) {
		delete pDialogYesNo;
		pDialogYesNo = null;
	}
}

//bool Lib9::Initialize() {
//	// Construct an XML form
//	//	Construct(L"IDF_LIB9");
//	//	Construct(FORM_STYLE_NORMAL);
//	//
//	//	FG = new Canvas();
//	//	FG->Construct(GetBounds());
//
//	return true;
//}

result Lib9::OnInitializing(void) {
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	AddKeyEventListener(*this);
	AddTouchEventListener(*this);
	return r;
}

result Lib9::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void Lib9::OnKeyLongPressed(const Osp::Ui::Control &source,
		Osp::Ui::KeyCode keyCode) {
	// TODO: Add your implementation codes here

}

void Lib9::OnKeyPressed(const Osp::Ui::Control &source,
		Osp::Ui::KeyCode keyCode) {
	// TODO: Add your implementation codes here
	logicKeyPressed(keyCode);
}

void Lib9::OnKeyReleased(const Osp::Ui::Control &source,
		Osp::Ui::KeyCode keyCode) {
	// TODO: Add your implementation codes here
	int keyFlag = getLogicKeyByKeyCode(keyCode);
	curKeyState |= keyFlag;
	curKeyPressed &= ~keyFlag;
}

void Lib9::OnTouchDoublePressed(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	// TODO: Add your implementation codes here

}

void Lib9::OnTouchFocusIn(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	// TODO: Add your implementation codes here

}

void Lib9::OnTouchFocusOut(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	// TODO: Add your implementation codes here

}

void Lib9::OnTouchLongPressed(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	// TODO: Add your implementation codes here

}

void Lib9::OnTouchMoved(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	// TODO: Add your implementation codes here
	dragPointerX = currentPosition.x;
	dragPointerY = currentPosition.y;
}

void Lib9::OnTouchPressed(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	// TODO: Add your implementation codes here
	curPointerX = currentPosition.x;
	curPointerY = currentPosition.y;
}

void Lib9::OnTouchReleased(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	// TODO: Add your implementation codes here
	dragPointerX = -1;
	dragPointerY = -1;
}

/////////////////////////////////////////按键系统//////////////////////////////////////////////////////////


/**
 * 调用该函数用来模拟按键效果，使用传统按键系统的游戏移动到触摸屏中为了减少工作量常常需要模拟按键效果
 * @param keyCode int
 */
void Lib9::logicKeyPressed(int keyCode) {
	int keyFlag = getLogicKeyByKeyCode(keyCode);

	curKeyReleased |= keyFlag;
	curKeyPressed |= keyFlag;
}

/**
 * 在帧驱动的应用或者游戏(帧驱动的方式往往是游戏)中每一帧都要清除按键值，isKeyPressed,isKeyReleased将会受到影响,而isKeyUp,isKeyDown则需要手动
 * 调用
 */
void Lib9::updateKey() {

	keyPressed = curKeyReleased;
	keyReleased = curKeyState;
	keyState = curKeyPressed;
	curKeyReleased = 0;
	curKeyState = 0;

	pointerX = curPointerX;
	pointerY = curPointerY;
	curPointerX = -1;
	curPointerY = -1;
}

/**
 * 清除按键值
 */
void Lib9::resetKey() {
	keyPressed = 0;
	keyReleased = 0;
	keyState = 0;
	curKeyPressed = 0;
	curKeyReleased = 0;
	curKeyState = 0;
}

/**
 * 判断某个键是否已经被释放了，注意参数是逻辑键值，这个函数与isKeyReleased的区别在于在应用程序的每一帧isKeyPressed对应的键值都会被清除，
 * 而isKeyDown则必须使用ResetKey或者ResetAKey进行清除
 * @param logicKey int
 * @return bool
 */
bool Lib9::isKeyDown(long long logicKey) {
	return (keyState & ((1LL) << logicKey)) != 0;
}

/**
 * 判断某个键是否已经被按下了，注意参数是逻辑键值，这个函数与isKeyPressed的区别在于在应用程序的每一帧isKeyPressed对应的键值都会被清除，
 * 而isKeyUp则必须使用ResetKey或者ResetAKey进行清除
 * @param logicKey int
 * @return bool
 */
bool Lib9::isKeyUp(long long logicKey) {
	return (keyState & ((1LL) << logicKey)) == 0;
}

/**
 * 判断是否按下了键
 * @return bool
 */
bool Lib9::isAnyKeyPressed() {
	for (int i = 0; i < nKEY_NUM; i++) {
		if ((keyPressed & ((1LL) << i)) != 0) {
			return true;
		}
	}
	return false;
}

/**
 * 判断是否有键被释放了
 * @return int
 */
bool Lib9::isAnyKeyReleased() {
	for (int i = 0; i < nKEY_NUM; i++) {
		if ((keyReleased & ((1LL) << i)) != 0) {
			return true;
		}
	}
	return false;
}

/**
 * 判断某个键是否已经被按下了，注意参数是逻辑键值
 * @param logicKey int
 * @return bool
 */
bool Lib9::isKeyPressed(long long logicKey) {
	return ((keyPressed & logicKey) != 0);
}

/**
 * 判断某个键是否被释放了，注意参数是逻辑键值
 * @param logicKey int
 * @return bool
 */
bool Lib9::isKeyReleased(long long logicKey) {
	return (keyReleased & logicKey) != 0;
}

/**
 * 根据键值返回键的逻辑值
 * @param keyCode int
 * @return int
 */
long long Lib9::getLogicKeyByKeyCode(int keyCode) {
	_keyCode = keyCode; // 用来检查按键
	if (keyCode >= KEY_0 && keyCode <= KEY_9) {
		return K_KEY_0 << (keyCode - K_KEY_0);
	} else if (keyCode >= KEY_A && keyCode <= KEY_Z) {
		return K_KEY_A << (keyCode - KEY_A);
	} else {
		switch (keyCode) {
		case KEY_UP:
			return K_KEY_UP;
		case KEY_DOWN:
			return K_KEY_DOWN;
		case KEY_LEFT:
			return K_KEY_LEFT;
		case KEY_RIGHT:
			return K_KEY_RIGHT;
		case KEY_OK:
			return K_KEY_OK;
		case KEY_SIDE_UP:
			return K_KEY_SIDE_UP;
		case KEY_SIDE_DOWN:
			return K_KEY_SIDE_DOWN;
		case KEY_CLEAR:
			return K_KEY_CLEAR;
		case KEY_CAMERA:
			return K_KEY_CAMERA;
		case KEY_SWITCH:
			return K_KEY_SWITCH;
		case KEY_ASTERISK:
			return K_KEY_ASTERISK;
		case KEY_SHARP:
			return K_KEY_SHARP;
		case KEY_BACKSPACE:
			return K_KEY_BACKSPACE;
		case KEY_COMMA:
			return K_KEY_COMMA;
		case KEY_ENTER:
			return K_KEY_ENTER;
		case KEY_CAPSLOCK:
			return K_KEY_CAPSLOCK;
		case KEY_QUESTION:
			return K_KEY_QUESTION;
		case KEY_ALT:
			return K_KEY_ALT;
		case KEY_SYM:
			return K_KEY_SYM;
		case KEY_SETTING:
			return K_KEY_SETTING;
		case KEY_SPACE:
			return K_KEY_SPACE;
		case KEY_DOT:
			return K_KEY_DOT;
		}
	}

	return 0L;
}

/**
 * 根据逻辑键返回按键值
 * @param logicKey int
 * @return int
 */
int Lib9::getKeyCodeByLogicKey(long long logicKey) {
	if ((logicKey & K_KEY_0) != 0) {
		return KEY_0;
	}
	if ((logicKey & K_KEY_1) != 0) {
		return KEY_1;
	}
	if ((logicKey & K_KEY_2) != 0) {
		return KEY_2;
	}
	if ((logicKey & K_KEY_3) != 0) {
		return KEY_3;
	}
	if ((logicKey & K_KEY_4) != 0) {
		return KEY_4;
	}
	if ((logicKey & K_KEY_5) != 0) {
		return KEY_5;
	}
	if ((logicKey & K_KEY_6) != 0) {
		return KEY_6;
	}
	if ((logicKey & K_KEY_7) != 0) {
		return KEY_7;
	}
	if ((logicKey & K_KEY_8) != 0) {
		return KEY_8;
	}
	if ((logicKey & K_KEY_9) != 0) {
		return KEY_9;
	}
	if ((logicKey & K_KEY_A) != 0) {
		return KEY_A;
	}
	if ((logicKey & K_KEY_B) != 0) {
		return KEY_B;
	}
	if ((logicKey & K_KEY_C) != 0) {
		return KEY_C;
	}
	if ((logicKey & K_KEY_D) != 0) {
		return KEY_D;
	}
	if ((logicKey & K_KEY_E) != 0) {
		return KEY_E;
	}
	if ((logicKey & K_KEY_F) != 0) {
		return KEY_F;
	}
	if ((logicKey & K_KEY_G) != 0) {
		return KEY_G;
	}
	if ((logicKey & K_KEY_H) != 0) {
		return KEY_H;
	}
	if ((logicKey & K_KEY_I) != 0) {
		return KEY_I;
	}
	if ((logicKey & K_KEY_J) != 0) {
		return KEY_J;
	}
	if ((logicKey & K_KEY_K) != 0) {
		return KEY_K;
	}
	if ((logicKey & K_KEY_L) != 0) {
		return KEY_L;
	}
	if ((logicKey & K_KEY_M) != 0) {
		return KEY_M;
	}
	if ((logicKey & K_KEY_N) != 0) {
		return KEY_N;
	}
	if ((logicKey & K_KEY_O) != 0) {
		return KEY_O;
	}
	if ((logicKey & K_KEY_P) != 0) {
		return KEY_P;
	}
	if ((logicKey & K_KEY_Q) != 0) {
		return KEY_Q;
	}
	if ((logicKey & K_KEY_R) != 0) {
		return KEY_R;
	}
	if ((logicKey & K_KEY_S) != 0) {
		return KEY_S;
	}
	if ((logicKey & K_KEY_T) != 0) {
		return KEY_T;
	}
	if ((logicKey & K_KEY_U) != 0) {
		return KEY_U;
	}
	if ((logicKey & K_KEY_V) != 0) {
		return KEY_V;
	}
	if ((logicKey & K_KEY_W) != 0) {
		return KEY_W;
	}
	if ((logicKey & K_KEY_X) != 0) {
		return KEY_X;
	}
	if ((logicKey & K_KEY_Y) != 0) {
		return KEY_Y;
	}
	if ((logicKey & K_KEY_Z) != 0) {
		return KEY_Z;
	}
	if ((logicKey & K_KEY_UP) != 0) {
		return KEY_UP;
	}
	if ((logicKey & K_KEY_DOWN) != 0) {
		return KEY_DOWN;
	}
	if ((logicKey & K_KEY_LEFT) != 0) {
		return KEY_LEFT;
	}
	if ((logicKey & K_KEY_RIGHT) != 0) {
		return KEY_RIGHT;
	}
	if ((logicKey & K_KEY_OK) != 0) {
		return KEY_OK;
	}
	if ((logicKey & K_KEY_SIDE_UP) != 0) {
		return KEY_SIDE_UP;
	}
	if ((logicKey & K_KEY_SIDE_DOWN) != 0) {
		return KEY_SIDE_DOWN;
	}
	if ((logicKey & K_KEY_CLEAR) != 0) {
		return KEY_CLEAR;
	}
	if ((logicKey & K_KEY_CAMERA) != 0) {
		return KEY_CAMERA;
	}
	if ((logicKey & K_KEY_SWITCH) != 0) {
		return KEY_SWITCH;
	}
	if ((logicKey & K_KEY_ASTERISK) != 0) {
		return KEY_ASTERISK;
	}
	if ((logicKey & K_KEY_SHARP) != 0) {
		return KEY_SHARP;
	}
	if ((logicKey & K_KEY_BACKSPACE) != 0) {
		return KEY_BACKSPACE;
	}
	if ((logicKey & K_KEY_COMMA) != 0) {
		return KEY_COMMA;
	}
	if ((logicKey & K_KEY_ENTER) != 0) {
		return KEY_ENTER;
	}
	if ((logicKey & K_KEY_CAPSLOCK) != 0) {
		return KEY_CAPSLOCK;
	}
	if ((logicKey & K_KEY_QUESTION) != 0) {
		return KEY_QUESTION;
	}
	if ((logicKey & K_KEY_ALT) != 0) {
		return KEY_ALT;
	}
	if ((logicKey & K_KEY_SYM) != 0) {
		return KEY_SYM;
	}
	if ((logicKey & K_KEY_SETTING) != 0) {
		return KEY_SETTING;
	}
	if ((logicKey & K_KEY_SPACE) != 0) {
		return KEY_SPACE;
	}
	if ((logicKey & K_KEY_DOT) != 0) {
		return KEY_DOT;
	}
	return 0;
}
/**
 * 判断是否处于拖动状态
 * @return bool
 */
bool Lib9::isDragPointer() {
	return dragPointerX != -1 && dragPointerY != -1;
}

/**
 * 返回触摸点的X坐标
 * @return int
 */
int Lib9::getPointerX() {
	return pointerX;
}

/**
 * 返回触摸点的Y坐标
 * @return int
 */
int Lib9::getPointerY() {
	return pointerY;
}

/**
 * 在处于拖动状态的时候返回当前点的X坐标
 * @return int
 */
int Lib9::getDragPointerX() {
	return dragPointerX;
}

/**
 * 在处于拖动状态的时候返回当前点的Y坐标
 * @return int
 */
int Lib9::getDragPointerY() {
	return dragPointerY;
}
/**
 * 返回帧时间,单位微妙
 *
 * @return
 */
long long Lib9::getTicks() {
	return m_l_DT;
}
Object* Lib9::Run() {
	//	while (true) {
	//		byte red = Math::Rand() % 0xFF;
	//		byte green = Math::Rand() % 0xFF;
	//		byte blue = Math::Rand() % 0xFF;
	//		byte alpha = Math::Rand() % 0xFF;
	//
	//		Color color(red, green, blue, alpha);
	//		FG->FillRectangle(color, Rectangle(0, 0, 200, 200));
	//		RequestRedraw(true);
	//		Thread::Sleep(25);
	//	}
	//	AppLog("run.");
	while (pAppState != null || !bFirstInitState) {
		//		AppLog("run.");
		if (!bAppPaused) {
			//                System.out.println("appState=" + appState);
			m_lBeginTime = System_currentTimeMillis();
			//				try {
			frameCount++;
			updateKey();
			appUpdate();

			//				repaint(0, 0, L9Config::SCR_W, L9Config::SCR_H);
			//				serviceRepaints();
			paint(*pFG);
			RequestRedraw(true);

			m_l_DT = System_currentTimeMillis() - m_lBeginTime;
			while (m_l_DT < m_frameRate) { //FRAME_RATE){
				Thread::Sleep(50);
				//                            Thread.yield();
				m_l_DT = System_currentTimeMillis() - m_lBeginTime;
			}

			if (L9Config::bShowFps) {
				if (m_l_DT <= 0) {
					m_l_DT = 1;
				}
				_fps = ((long) 1000) / m_l_DT;
			}
			//          System.out.println("m_frameRate="+m_frameRate);
			//				} catch (Exception e) {
			//					e.printStackTrace();
			//				}
		}
		//      System.out.println("list_elements_index="+List_Elements_Index);
	} //while App_State
	return null;
}
bool Lib9::OnStart(void) {
	return true;
}
void Lib9::OnStop(void) {

}
result Lib9::OnDraw(void) {
	Canvas* pCanvas = GetCanvasN();
	// For Double Buffering
	if (pCanvas) {
		pCanvas->Copy(Point(GetClientAreaBounds().x, GetClientAreaBounds().y),
				*pFG, GetClientAreaBounds());
		delete pCanvas;
	}
	pFG->Clear();
	return E_SUCCESS;
}
//////////////////////////State Manager//////////////////////////////////////////////

/**
 * 将状态机的当前状态入栈
 */
void Lib9::pushState() {
	pushState(pAppState);
}

/**
 * 将状态state入栈
 * @param state L9IState
 */
void Lib9::pushState(L9IState* state) {
	sState.push(state);
}

/**
 * 将状态出栈，返回出栈的状态,如果堆栈为空将返回null
 * @return L9IState
 */
L9IState* Lib9::popState() {
	L9IState* state = null;
	if (sState.size() > 0) {
		state = sState.top();
		sState.pop();
	}
	return state;
}

/**
 * 返回出栈的状态,但是不出栈,如果堆栈为空将返回null
 * @return L9IState
 */
L9IState* Lib9::topState() {
	return sState.top();
}

/**
 * 返回状态堆栈的大小
 * @return int
 */
int Lib9::getStateSize() {
	return sState.size();
}

/**
 * 清空状态机堆栈
 */
void Lib9::clearState() {
	while (!sState.empty()) {
		sState.pop();
	}
}
/**
 * 用来切换状态机的状态,如果state为null,将退出程序
 * @param state L9IState
 */
void Lib9::changeState(L9IState* state) {
	if (state == null) {
		quitApp();
		return;
	}
	pAppState = state;
	resetKey(); //状态改变时应该清除按键
	state->Init();
	//表示已经进入到具体的状态了
	if (!bFirstInitState) {
		bFirstInitState = true;
	}
}
/**
 * 必须实现的抽象类方法，用来执行应用的程序逻辑
 */
void Lib9::appUpdate() {
	if (pAppState != null) {
		pAppState->Update();
	}
}

/**
 * 必须实现的抽象类方法，绘制应用的画面
 */
void Lib9::appPaint() {
	if (pAppState != null) {
		pAppState->Paint();
	}
}
void Lib9::defaultInit() {
	bAppPaused = false;
	bFirstInitState = false;
	//	imgBuffer = null;
	bAppInPainting = false;
	bUseGlobalGraphics = false;
	//		sState = new Stack();

	pointerX = -1;
	pointerY = -1;
	curPointerX = -1;
	curPointerY = -1;

	dragPointerX = -1;
	dragPointerY = -1;

	_keyCode = -1;

	_fps = 15;

	pFG = null;

	frameCount = 0;

	setFps(L9Config::appFps);

	pApp = Application::GetInstance();
}
long long Lib9::System_currentTimeMillis() {
	long long Net_BeginTime;
	SystemTime::GetTicks(Net_BeginTime);
	return Net_BeginTime;
}
/**
 * 设置程序的fps,就是每秒钟显示的帧数
 * @param fps int
 */
void Lib9::setFps(int fps) {
	_fps = fps;
	m_frameRate = ((long) 1000) / _fps;
}
/**
 * 设置程序和设置程序的显示容器
 * @param app MIDlet
 * @param disp Displayable
 */
void Lib9::setDisplay(Application* app, Form* disp) {
	if (pLastAppDisplay != disp) {
		pApp = app;
		pAppDisplay = disp;
		//记录上一次的显示容器
		Frame *pFrame = app->GetAppFrame()->GetFrame();
		pLastAppDisplay = pFrame->GetCurrentForm();

		pFrame->AddControl(*disp);
		pFrame->SetCurrentForm(*disp);
		//		setFullScreenMode(true);

		// Draw and Show the form
		Draw();
		Show();
	}
}

/**
 * 切换到上一次的显示容器，调用setDisplay设置显示容器的时候将会记录上一次显示容器，调用backLastDisplay返回上一次显示容器后会将lastAppDisplay=null
 */
void Lib9::backLastDisplay() {
	if (pLastAppDisplay != null) {
		setDisplay(pApp, pLastAppDisplay);
		pLastAppDisplay = null;
	}
}
/**
 * 在使用双缓冲(即L9Config::bUseDoubleBuffer为true)的情况下，使用全局的Graphics
 * @param bUse bool
 */
void Lib9::setGlobalGraphics(bool bUse) {
	bUseGlobalGraphics = bUse;
}

/**
 * 在使用双缓冲(即L9Config::bUseDoubleBuffer为true)且使用全局的Graphics的情况下用来绘制缓存图片的内容
 */
void Lib9::drawBufferImage() {
	//	if (bUseGlobalGraphics && L9Config::bUseDoubleBuffer) {
	//		FG->DrawBitmap(Point(0, 0), *imgBuffer);
	//	}
}
void Lib9::fillScreen(int color, int x, int y, int w, int h) {
	pFG->SetForegroundColor(Color(color));
	pFG->FillRectangle(Color(color), Rectangle(x, y, w, h));
}
/**
 * 清屏操作，用color颜色值来填充整个屏幕
 * @param color int
 */
void Lib9::fillScreen(int color) {
	fillScreen(color, 0, 0, SCR_W, SCR_H);
}
void Lib9::paint(Canvas& g) {
	//	if (L9Config::bUseFakeInterrupt) { //软中断处理
	//		long long dt = System_currentTimeMillis() - lastFrameTime;
	//		lastFrameTime = System_currentTimeMillis();
	//		if (dt > L9Config::FakeInterruptWait) {
	//			pauseApp();
	//			resumeApp();
	//		}
	//	}
	if (bAppPaused || bAppInPainting) { //暂停或者正在绘制中
		return;
	}
	bAppInPainting = true;

	//		if (L9Config::bUseDoubleBuffer && !bUseGlobalGraphics) { //双缓冲
	//			if (imgBuffer == null) {
	//				return;
	//			}
	//			FG = imgBuffer.getGraphics(); // FBackImage_G;
	//		} else {
	//			FG = g;
	//		}

	appPaint();

	if (L9Config::bUseDoubleBuffer && !bUseGlobalGraphics) {
		//			g.drawImage(imgBuffer, 0, 0, 0);
		//		g.DrawBitmap(Point(0, 0), *imgBuffer);
	}
	//		if (L9Config::bShowMemory) {
	//			g.setColor(0xffffff);
	//			g.fillRect(L9Config::SCR_W - 100, 0, 100, L9Config::FONT_H);
	//			g.setColor(0);
	//			String str = "" + Runtime.getRuntime().freeMemory() + "/"
	//					+ Runtime.getRuntime().totalMemory();
	//			g.drawString(str, L9Config::SCR_W - 0, 0, g.TOP | g.RIGHT);
	//		}

	if (L9Config::bShowFps) {
		//			g.setColor(0xffffff);
		//			g.fillRect(0, 0, L9Config::SCR_W, 3 * L9Config::FONT_H);
		//			g.setColor(0);
		//			g.drawString("fps:" + _fps, 0, 0, g.TOP | g.LEFT);
		fillScreen(0xFFFFFFFF, 0, 0, SCR_W, 24);
		g.SetForegroundColor(Color(0xFF000000));
		String sFps = L"fps:";
		sFps.Append(_fps);
		g.DrawText(Point(0, 0), sFps);
	}
	if (L9Config::bShowParam) {
		//			String str = "屏幕:" + getWidth() + "X" + getHeight() + " 键值:"
		//					+ _keyCode;
		//			g.setColor(0xffffff);
		//			g.fillRect(0, 0, 160, L9Config::FONT_H);
		//			g.setColor(0);
		//			g.drawString(str, 0, 0, g.TOP | g.LEFT);

		String sW, sH;
		SystemInfo::GetValue("ScreenWidth", sW);
		SystemInfo::GetValue("ScreenHeight", sH);

		String str = L"屏幕:";
		str.Append(sW);
		str.Append("X");
		str.Append(sH);
		str.Append(" 键值:");
		str.Append(_keyCode);
		fillScreen(0xFFFFFFFF, 0, 0, SCR_W, 24);
		g.SetForegroundColor(Color(0xFF000000));
		g.DrawText(Point(0, 0), str);
	}

	frameCount++;
	bInterruptNotify = false;
	bAppInPainting = false;
}
/**
 * 判断某点是否在某个线段之间
 * @param pX int
 * @param leftX int
 * @param rightX int
 * @return bool
 */
bool Lib9::isInLine(int pX, int leftX, int rightX) {
	return pX >= leftX && pX < rightX;
}

/**
 * 判断某个点是否在矩形中，参数rect表示矩形的左上角和右下角两点坐标
 * @param pX int
 * @param pY int
 * @param rect int[]
 * @return bool
 */
bool Lib9::isInRect(int pX, int pY, L9Rect rect) {
	return isInLine(pX, rect.Left, rect.Right) && isInLine(pY, rect.Top,
			rect.Bottom);
}

//////////////////////////////////////////////消息对话框/////////////////////////////////////////////////////////////
/**
 * 显示消息对话框，默认居中显示，如果是触摸屏的话，可支持拖动
 * @param sTitle String
 * @param strMsg String
 * @param btnText String
 * @param Dialog_W int
 */
void Lib9::showMsgDialog(String sTitle, String strMsg, String btnText,
		int Dialog_W) {
	pushState();
	setGlobalGraphics(true);
	if (pDialogMsg == null) {
		pDialogMsg = new L9DialogMsg(this);
	}
	pDialogMsg->setMsgDialog(sTitle, strMsg, btnText, Dialog_W);
	changeState(pDialogMsg);
}

/**
 * 显示确认对话框，默认居中显示，如果是触摸屏的话，可支持拖动
 * @param sTitle String
 * @param strMsg String
 * @param sYes String
 * @param sNo String
 * @param Dialog_W int
 */
void Lib9::showYesNoDialog(String sTitle, String strMsg, String sYes,
		String sNo, int Dialog_W) {
	pushState();
	setGlobalGraphics(true);
	if (pDialogYesNo == null) {
		pDialogYesNo = new L9DialogYesNo(this);
	}
	pDialogYesNo->setYesNoDialog(sTitle, strMsg, sYes, sNo, Dialog_W);
	changeState(pDialogYesNo);
}
/**
 * 退出程序
 */
void Lib9::quitApp() {
	pApp->Terminate();
	pAppState = null;
}
};

